package com.shang.controller;

import com.shang.pojo.UserAddress;
import com.shang.pojo.bo.AddressBO;
import com.shang.service.AddressService;
import com.shang.utils.MobileEmailUtils;
import com.shang.utils.ShangJSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "收货地址")
@RequestMapping("address")
@RestController
public class AddressController {


    /**
     * 订单确认页面
     *      查询地址列表
     *      新增地址
     *      删除地址
     *      修改地址
     */

    @Autowired
    private AddressService addressService;

    @ApiOperation(value = "地址查询")
    @PostMapping("/list")
    public ShangJSONResult list(@RequestParam String userId){

        if (StringUtils.isBlank(userId)){
            return ShangJSONResult.errorMsg("");
        }

        List<UserAddress> list = addressService.queryAll(userId);

        return ShangJSONResult.ok(list);
    }


    @ApiOperation(value = "地址新增")
    @PostMapping("/add")
    public ShangJSONResult add(@RequestBody AddressBO addressBO){

        String receiver = addressBO.getReceiver();
        if (StringUtils.isBlank(receiver)){
            return ShangJSONResult.errorMsg("收货人不能为空");
        }

        if (receiver.length() > 12){
            return ShangJSONResult.errorMsg("收货人姓名过长");
        }
        String mobile = addressBO.getMobile();
        if (StringUtils.isBlank(mobile)){
            return ShangJSONResult.errorMsg("手机号不能为空");
        }
        if (mobile.length() != 11){
            return ShangJSONResult.errorMsg("手机号格式错误");
        }
        boolean mobileIsOk = MobileEmailUtils.checkMobileIsOk(mobile);
        if (!mobileIsOk){
            return ShangJSONResult.errorMsg("手机号格式错误");
        }

        String province = addressBO.getProvince();
        String city = addressBO.getCity();
        String district = addressBO.getDistrict();
        String detail = addressBO.getDetail();
        if (StringUtils.isBlank(province) || StringUtils.isBlank(city) || StringUtils.isBlank(district) || StringUtils.isBlank(detail)){
            return ShangJSONResult.errorMsg("地址不能为空");
        }

        addressService.addNewUserAddress(addressBO);
        return ShangJSONResult.ok();
    }


    @ApiOperation(value = "地址新增")
    @PostMapping("/update")
    public ShangJSONResult update(@RequestBody AddressBO addressBO){

        if (StringUtils.isBlank(addressBO.getAddressId())){
            return ShangJSONResult.errorMsg("");
        }

        String receiver = addressBO.getReceiver();
        if (StringUtils.isBlank(receiver)){
            return ShangJSONResult.errorMsg("收货人不能为空");
        }

        if (receiver.length() > 12){
            return ShangJSONResult.errorMsg("收货人姓名过长");
        }
        String mobile = addressBO.getMobile();
        if (StringUtils.isBlank(mobile)){
            return ShangJSONResult.errorMsg("手机号不能为空");
        }
        if (mobile.length() != 11){
            return ShangJSONResult.errorMsg("手机号格式错误");
        }
        boolean mobileIsOk = MobileEmailUtils.checkMobileIsOk(mobile);
        if (!mobileIsOk){
            return ShangJSONResult.errorMsg("手机号格式错误");
        }

        String province = addressBO.getProvince();
        String city = addressBO.getCity();
        String district = addressBO.getDistrict();
        String detail = addressBO.getDetail();
        if (StringUtils.isBlank(province) || StringUtils.isBlank(city) || StringUtils.isBlank(district) || StringUtils.isBlank(detail)){
            return ShangJSONResult.errorMsg("地址不能为空");
        }

        addressService.updateUserAddress(addressBO);
        return ShangJSONResult.ok();
    }


    @ApiOperation(value = "地址删除")
    @PostMapping("/delete")
    public ShangJSONResult delete(@RequestParam String userId,@RequestParam String addressId){

        if (StringUtils.isBlank(userId)){
            return ShangJSONResult.errorMsg("");
        }

        if (StringUtils.isBlank(addressId)){
            return ShangJSONResult.errorMsg("");
        }

        addressService.deleteUserAddress(userId, addressId);
        return ShangJSONResult.ok();
    }


    @ApiOperation(value = "地址默认更新")
    @PostMapping("/setDefalut")
    public ShangJSONResult setDefalut(@RequestParam String userId,@RequestParam String addressId){

        if (StringUtils.isBlank(userId)){
            return ShangJSONResult.errorMsg("");
        }

        if (StringUtils.isBlank(addressId)){
            return ShangJSONResult.errorMsg("");
        }

        addressService.updateUserAddressToBeDefault(userId, addressId);
        return ShangJSONResult.ok();
    }

}
